package com.mdd.car.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.car.service.IVehicleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.car.entity.vehicle.Vehicle;
import com.mdd.car.mapper.vehicle.VehicleMapper;
import com.mdd.car.params.vehicle.VehicleCreateValidate;
import com.mdd.car.params.vehicle.VehicleSearchValidate;
import com.mdd.car.params.vehicle.VehicleUpdateStatusValidate;
import com.mdd.car.params.vehicle.VehicleUpdateValidate;
import com.mdd.car.vo.ProductSkuDetailVo;
import com.mdd.car.vo.ProductSpuDetailVo;
import com.mdd.car.vo.StoreDetailVo;
import com.mdd.car.vo.vehicle.VehicleDetailVo;
import com.mdd.car.vo.vehicle.VehicleVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OrderItem;
import com.mdd.common.mapper.order.OrderItemMapper;
import com.mdd.common.util.StringUtils;
import com.mdd.product.entity.ProductSku;
import com.mdd.product.entity.ProductSpu;
import com.mdd.product.mapper.ProductSkuMapper;
import com.mdd.product.mapper.ProductSpuMapper;
import com.mdd.store.entity.Store;
import com.mdd.store.mapper.StoreMapper;
import com.mdd.system.mapper.dict.DictDataMapper;
import com.mdd.system.vo.SystemAuthAdminInformVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 车辆实现类
 * @author LikeAdmin
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper,Vehicle> implements IVehicleService {

    @Resource
    VehicleMapper mvaVehicleMapper;
    @Resource
    StoreMapper storeMapper;
    @Resource
    ProductSpuMapper productSpuMapper;
    @Resource
    ProductSkuMapper productSkuMapper;
    @Resource
    DictDataMapper dictDataMapper;
    @Resource
    OrderItemMapper orderItemMapper;
    /**
     * 车辆列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<VehicleListedVo>
     */
    @Override
    public PageResult<VehicleVo> list(PageValidate pageValidate, VehicleSearchValidate searchValidate, SystemAuthAdminInformVo systemAuthAdmin) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Vehicle> queryWrapper = new QueryWrapper<>();
        if (!systemAuthAdmin.getDeptIds().equals(1L)) {
            queryWrapper.likeRight("dept_code", systemAuthAdmin.getDeptCode());
        }

        if(StringUtils.isNotEmpty(searchValidate.getLicensePlate())) {
            queryWrapper.likeRight("license_plate", searchValidate.getLicensePlate());
        }
        if(StringUtils.isNotEmpty(searchValidate.getVehicleName())) {
            queryWrapper.likeRight("vehicle_name", searchValidate.getVehicleName());
        }
        if(searchValidate.getStoreId()!=null) {
            queryWrapper.eq("store_id", searchValidate.getStoreId());
        }
        if(searchValidate.getRentalStatus()!=null) {
            queryWrapper.eq("rental_status", searchValidate.getRentalStatus());
        }
        if(searchValidate.getVehicleStatus()!=null) {
            queryWrapper.eq("vehicle_status", searchValidate.getVehicleStatus());
        }
        queryWrapper.orderByDesc("id");

        IPage<Vehicle> iPage = mvaVehicleMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<VehicleVo> list = new LinkedList<>();
        for(Vehicle item : iPage.getRecords()) {
            VehicleVo vo = new VehicleVo();
            BeanUtils.copyProperties(item, vo);
            vo.setColor(dictDataMapper.selectById(item.getColor()));
            vo.setVehicleType(dictDataMapper.selectById(item.getVehicleType()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public PageResult<VehicleVo> unBindList(PageValidate pageValidate, VehicleSearchValidate searchValidate,SystemAuthAdminInformVo systemAuthAdmin) {
        OrderItem orderItem = orderItemMapper.selectOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId,searchValidate.getOrderId()));

        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(Vehicle::getDeptCode, systemAuthAdmin.getDeptCode());
        queryWrapper.eq(Vehicle::getProductId,orderItem.getProductId())
        .eq(Vehicle::getVehicleStatus, 0)
        .eq(Vehicle::getRentalStatus, 0)
        .orderByDesc(Vehicle::getId);

        IPage<Vehicle> iPage = mvaVehicleMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<VehicleVo> list = new LinkedList<>();
        for(Vehicle item : iPage.getRecords()) {
            VehicleVo vo = new VehicleVo();
            BeanUtils.copyProperties(item, vo);
            vo.setColor(dictDataMapper.selectById(item.getColor()));
            vo.setVehicleType(dictDataMapper.selectById(item.getVehicleType()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);

    }

    /**
     * 车辆详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Vehicle
     */
    @Override
    public VehicleDetailVo detail(Long id) {
        Vehicle model = mvaVehicleMapper.selectOne(
                new QueryWrapper<Vehicle>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "车辆数据不存在");

        VehicleDetailVo vo = new VehicleDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setColor(dictDataMapper.selectById(model.getColor()));
        vo.setVehicleType(dictDataMapper.selectById(model.getVehicleType()));

        Store store = storeMapper.selectById(model.getStoreId());
        Assert.notNull(store, "门店数据不存在");
        StoreDetailVo storeDetailVo = new StoreDetailVo();
        BeanUtils.copyProperties(store, storeDetailVo);
        vo.setStore(storeDetailVo);

        ProductSku productSku = productSkuMapper.selectById(model.getSkuId());
        //Assert.notNull(productSku, "sku数据不存在");
        if(Objects.nonNull(productSku)) {
            ProductSkuDetailVo productSkuDetailVo = new ProductSkuDetailVo();
            BeanUtils.copyProperties(productSku, productSkuDetailVo);
            vo.setProductSku(productSkuDetailVo);
        }

        ProductSpu productSpu = productSpuMapper.selectById(model.getProductId());
       // Assert.notNull(productSpu, "spu数据不存在");
        if(Objects.nonNull(productSpu)) {
            ProductSpuDetailVo productSpuDetailVo = new ProductSpuDetailVo();
            BeanUtils.copyProperties(productSpu, productSpuDetailVo);
            vo.setProductSpu(productSpuDetailVo);
        }
        return vo;
    }

    /**
     * 车辆新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(VehicleCreateValidate createValidate) {

        //获取门店
        Store pmsStore = storeMapper.selectById(createValidate.getStoreId());
        Assert.notNull(pmsStore, "门店不存在");
        //获取车品
        ProductSpu storeProduct = productSpuMapper.selectById(createValidate.getProductId());
        Assert.notNull(storeProduct, "车品不存在");
        //获取车品sku
        ProductSku storeProductAttrValue = productSkuMapper.selectById(createValidate.getSkuId());
        Assert.notNull(storeProductAttrValue, "sku不存在");

        Vehicle model = new Vehicle();
        model.setVehicleNo(createValidate.getVehicleNo());
        model.setStoreId(createValidate.getStoreId());
        model.setColor(createValidate.getColor());
        model.setEngineNumber(createValidate.getEngineNumber());
        model.setOperationType(createValidate.getOperationType());
        model.setLicensePlate(createValidate.getLicensePlate());
        model.setRentalStatus(createValidate.getRentalStatus());
        model.setVehicleStatus(createValidate.getVehicleStatus());
        model.setVehicleSrc(createValidate.getVehicleSrc());
        model.setVin(createValidate.getVin());
        model.setVehicleType(createValidate.getVehicleType());
        model.setSkuId(createValidate.getSkuId());
        model.setProductId(createValidate.getProductId());
        model.setVehicleName(storeProduct.getName());
        model.setDeptCode(pmsStore.getDeptCode());
        mvaVehicleMapper.insert(model);
    }

    /**
     * 车辆编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(VehicleUpdateValidate updateValidate) {
        Vehicle model = mvaVehicleMapper.selectOne(
                new QueryWrapper<Vehicle>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "车辆数据不存在!");

        //获取门店
        Store pmsStore = storeMapper.selectById(updateValidate.getStoreId());
        Assert.notNull(pmsStore, "门店不存在");
        //获取车品
        ProductSpu storeProduct = productSpuMapper.selectById(updateValidate.getProductId());
        Assert.notNull(storeProduct, "车品不存在");
        //获取车品sku
        ProductSku productSku = productSkuMapper.selectById(updateValidate.getSkuId());
        Assert.notNull(productSku, "sku不存在");

        model.setId(updateValidate.getId());
        model.setVehicleNo(updateValidate.getVehicleNo());
        model.setStoreId(updateValidate.getStoreId());
        model.setColor(updateValidate.getColor());
        model.setEngineNumber(updateValidate.getEngineNumber());
        model.setOperationType(updateValidate.getOperationType());
        model.setLicensePlate(updateValidate.getLicensePlate());
        model.setRentalStatus(updateValidate.getRentalStatus());
        model.setVehicleStatus(updateValidate.getVehicleStatus());
        model.setVehicleSrc(updateValidate.getVehicleSrc());
        model.setVin(updateValidate.getVin());
        model.setVehicleType(updateValidate.getVehicleType());
        model.setVehicleName(storeProduct.getName());
        model.setSkuId(productSku.getId());
        model.setProductId(storeProduct.getId());
        mvaVehicleMapper.updateById(model);
    }

    @Override
    public void updateStatus(VehicleUpdateStatusValidate updateStatusValidate) {
        Vehicle model = mvaVehicleMapper.selectOne(
                new QueryWrapper<Vehicle>()
                        .eq("id", updateStatusValidate.getId())
                        .last("limit 1"));

        Assert.notNull(model, "车辆数据不存在!");
        model.setRentalStatus(updateStatusValidate.getRentalStatus());
        model.setVehicleStatus(updateStatusValidate.getVehicleStatus());
        mvaVehicleMapper.updateById(model);
    }

    /**
     * 车辆删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        Vehicle model = mvaVehicleMapper.selectOne(
                new QueryWrapper<Vehicle>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "车辆数据不存在!");
        //  mvaVehicleMapper.updateById(model);
      mvaVehicleMapper.deleteById(id);
    }

}
